package com.fullsemi.mitgeneralservice.service.impl;

import com.fullsemi.mitgeneralservice.common.utils.StringUtils;
import com.fullsemi.mitgeneralservice.entity.User;
import com.fullsemi.mitgeneralservice.mapper.UserMapper;
import com.fullsemi.mitgeneralservice.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public boolean checkEmployNoExist(User user) {
        if (StringUtils.isEmpty(user.getEmployeeNo()))
            return false;
        List<User> users = userMapper.selectUserByEmployeeNo(user.getEmployeeNo());
        if (users.size() > 1)
            return true;
        if (users.size() == 1 && !(Objects.equals(users.get(0).getId(), user.getId())))
            return true;
        return false;
    }

    @Override
    public boolean checkEmailExist(User user) {
        if (StringUtils.isEmpty(user.getEmail()))
            return false;
        List<User> users = userMapper.selectUserByEmail(user.getEmail());
        if (users.size() > 1)
            return true;
        if (users.size() == 1 && !(Objects.equals(users.get(0).getId(), user.getId())))
            return true;
        return false;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    @Transactional
    public int updateUser(User user) {
        Integer id = user.getId();
        return userMapper.updateUser(user);
    }


    @Override
    @Transactional
    public int deleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    @Transactional
    public int deleteUserByIds(Integer[] ids) {
        return userMapper.deleteUserByIds(ids);
    }
}
